//
//  RGLoginViewModel.m
//  RedGrass
//
//  Created by wukong on 2018/12/29.
//  Copyright © 2018年 hongcaosp. All rights reserved.
//

#import "RGLoginViewModel.h"
#import "RGLoginInterfaceBulider.h"

@interface RGLoginViewModel()

@property (weak, nonatomic) id<RGLoginViewProtocol> protocol;

@end

@implementation RGLoginViewModel

-(instancetype)initWithProtocol:(id<RGLoginViewProtocol>)protocol{
    if(self = [super init]){
        self.protocol = protocol;
    }
    return self;
}

//获取验证码
-(void) gainCaptchaCodeWithPhone:(NSString *)phone{
    AA_WEAK_MAKE(self)weakSelf = self;
    [self.protocol showLoading:@""];
    HttpRequest *quest = [HttpRequest buildRequest:url_sendCode];
    NSDictionary *params = [RGLoginInterfaceBulider bulidGainCaptchaParamsDicWithPhone:phone type:1];
    [quest post:params success:^(id data) {
        [weakSelf.protocol dismissLoading];
        [weakSelf.protocol showTextMessageWithMessage:@"发送成功"];
        [weakSelf.protocol ViewBegainGainCodeTimer];
    } fail:^(NSInteger code, NSString *message) {
        [weakSelf.protocol showTextMessageWithMessage:message];
        [weakSelf.protocol loginWithCaptchaCodeSuccess];
    }];
}

//验证码登陆
-(void) loginWithCaptchaCode:(NSString *)code phone:(NSString *)phone{
    AA_WEAK_MAKE(self) weakSelf = self;
    [self.protocol showLoading:@"正在登陆"];
    HttpRequest *quest = [HttpRequest buildRequest:@"/api/userLogin"];
    NSDictionary *params = @{@"mobile":SafeStr(phone),
                             @"verificationCode":SafeStr(code),
                             @"type":@(0)
                             };
    [quest post:params success:^(id data) {
        [weakSelf.protocol dismissLoading];
        NSDictionary *dic = [data objectForKey:@"userInfo"];
        if([dic count]){
            NSError *error;
            RGUserInfoModel *user = [[RGUserInfoModel alloc]initWithDictionary:dic error:&error];
            if(error == nil){
                [RGUserInfoModel storeUserInfo:user];
                [weakSelf.protocol loginWithCaptchaCodeSuccess];
            }else{
                  [weakSelf.protocol showTextMessageWithMessage:@"登录失败"];
            }
        }else{
              [weakSelf.protocol showTextMessageWithMessage:@"登录失败"];
        }
       
    } fail:^(NSInteger code, NSString *message) {
        [weakSelf.protocol showTextMessageWithMessage:message];
    }];
}

//微信登录
-(void)loginWithWechatWithCode:(NSString *)code{
    AA_WEAK_MAKE(self) weakSelf = self;
    [self.protocol showLoading:@"正在登陆"];
    HttpRequest *quest = [HttpRequest buildRequest:@"/api/userLogin"];
    NSDictionary *params = @{
                             @"loginCode":SafeStr(code),
                             @"type":@(2)
                             };
    [quest post:params success:^(id data) {
        [weakSelf.protocol dismissLoading];
        NSDictionary *dic = [data objectForKey:@"userInfo"];
        if([dic count]){
            NSError *error;
            RGUserInfoModel *user = [[RGUserInfoModel alloc]initWithDictionary:dic error:&error];
            if(error == nil){
                [RGUserInfoModel storeUserInfo:user];
                [weakSelf.protocol loginWithCaptchaCodeSuccess];
            }else{
                [weakSelf.protocol showTextMessageWithMessage:@"登录失败"];
            }
        }else{
            [weakSelf.protocol showTextMessageWithMessage:@"登录失败"];
        }
        
    } fail:^(NSInteger code, NSString *message) {
        [weakSelf.protocol showTextMessageWithMessage:message];
    }];
}


@end
